<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>variantdelegate.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-tools-settingseditor-example.html">Settings Editor Example</a></td><td >variantdelegate.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">variantdelegate.cpp 示例文件</h1>
<span class="subtitle">tools/settingseditor/variantdelegate.cpp</span>
<!-- $$$tools/settingseditor/variantdelegate.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="preprocessor">#include &quot;variantdelegate.h&quot;</span>

  VariantDelegate<span class="operator">::</span>VariantDelegate(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qitemdelegate.html">QItemDelegate</a></span>(parent)
  {
      boolExp<span class="operator">.</span>setPattern(<span class="string">&quot;true|false&quot;</span>);
      boolExp<span class="operator">.</span>setPatternOptions(<span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span><span class="operator">::</span>CaseInsensitiveOption);

      byteArrayExp<span class="operator">.</span>setPattern(<span class="string">&quot;[\\x00-\\xff]*&quot;</span>);
      charExp<span class="operator">.</span>setPattern(<span class="string">&quot;.&quot;</span>);
      colorExp<span class="operator">.</span>setPattern(<span class="string">&quot;^\\(([0-9]*),([0-9]*),([0-9]*),([0-9]*)\\)$&quot;</span>);
      doubleExp<span class="operator">.</span>setPattern(<span class="string">&quot;&quot;</span>);
      pointExp<span class="operator">.</span>setPattern(<span class="string">&quot;^\\((-?[0-9]*),(-?[0-9]*)\\)$&quot;</span>);
      rectExp<span class="operator">.</span>setPattern(<span class="string">&quot;^\\((-?[0-9]*),(-?[0-9]*),(-?[0-9]*),(-?[0-9]*)\\)$&quot;</span>);
      signedIntegerExp<span class="operator">.</span>setPattern(<span class="string">&quot;-?[0-9]*&quot;</span>);
      sizeExp <span class="operator">=</span> pointExp;
      unsignedIntegerExp<span class="operator">.</span>setPattern(<span class="string">&quot;[0-9]*&quot;</span>);

      dateExp<span class="operator">.</span>setPattern(<span class="string">&quot;([0-9]{,4})-([0-9]{,2})-([0-9]{,2})&quot;</span>);
      timeExp<span class="operator">.</span>setPattern(<span class="string">&quot;([0-9]{,2}):([0-9]{,2}):([0-9]{,2})&quot;</span>);
      dateTimeExp<span class="operator">.</span>setPattern(dateExp<span class="operator">.</span>pattern() <span class="operator">+</span> <span class="char">'T'</span> <span class="operator">+</span> timeExp<span class="operator">.</span>pattern());
  }

  <span class="type">void</span> VariantDelegate<span class="operator">::</span>paint(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">*</span>painter<span class="operator">,</span>
                              <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&amp;</span>option<span class="operator">,</span>
                              <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">=</span><span class="operator">=</span> <span class="number">2</span>) {
          <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> value <span class="operator">=</span> index<span class="operator">.</span>model()<span class="operator">-</span><span class="operator">&gt;</span>data(index<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
          <span class="keyword">if</span> (<span class="operator">!</span>isSupportedType(value<span class="operator">.</span>type())) {
              <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> myOption <span class="operator">=</span> option;
              myOption<span class="operator">.</span>state <span class="operator">&amp;</span><span class="operator">=</span> <span class="operator">~</span><span class="type"><a href="qstyle.html">QStyle</a></span><span class="operator">::</span>State_Enabled;
              <span class="type"><a href="qitemdelegate.html">QItemDelegate</a></span><span class="operator">::</span>paint(painter<span class="operator">,</span> myOption<span class="operator">,</span> index);
              <span class="keyword">return</span>;
          }
      }

      <span class="type"><a href="qitemdelegate.html">QItemDelegate</a></span><span class="operator">::</span>paint(painter<span class="operator">,</span> option<span class="operator">,</span> index);
  }

  <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>VariantDelegate<span class="operator">::</span>createEditor(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent<span class="operator">,</span>
          <span class="keyword">const</span> <span class="type"><a href="qstyleoptionviewitem.html">QStyleOptionViewItem</a></span> <span class="operator">&amp;</span> <span class="comment">/* option */</span><span class="operator">,</span>
          <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span>
  {
      <span class="keyword">if</span> (index<span class="operator">.</span>column() <span class="operator">!</span><span class="operator">=</span> <span class="number">2</span>)
          <span class="keyword">return</span> <span class="number">0</span>;

      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> originalValue <span class="operator">=</span> index<span class="operator">.</span>model()<span class="operator">-</span><span class="operator">&gt;</span>data(index<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
      <span class="keyword">if</span> (<span class="operator">!</span>isSupportedType(originalValue<span class="operator">.</span>type()))
          <span class="keyword">return</span> <span class="number">0</span>;

      <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>lineEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(parent);
      lineEdit<span class="operator">-</span><span class="operator">&gt;</span>setFrame(<span class="keyword">false</span>);

      <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> regExp;

      <span class="keyword">switch</span> (originalValue<span class="operator">.</span>type()) {
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Bool:
          regExp <span class="operator">=</span> boolExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>ByteArray:
          regExp <span class="operator">=</span> byteArrayExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Char:
          regExp <span class="operator">=</span> charExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Color:
          regExp <span class="operator">=</span> colorExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Date:
          regExp <span class="operator">=</span> dateExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>DateTime:
          regExp <span class="operator">=</span> dateTimeExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Double:
          regExp <span class="operator">=</span> doubleExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Int:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>LongLong:
          regExp <span class="operator">=</span> signedIntegerExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Point:
          regExp <span class="operator">=</span> pointExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Rect:
          regExp <span class="operator">=</span> rectExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Size:
          regExp <span class="operator">=</span> sizeExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Time:
          regExp <span class="operator">=</span> timeExp;
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>UInt:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>ULongLong:
          regExp <span class="operator">=</span> unsignedIntegerExp;
          <span class="keyword">break</span>;
      <span class="keyword">default</span>:
          ;
      }

      <span class="keyword">if</span> (regExp<span class="operator">.</span>isValid()) {
          <span class="type"><a href="../qtgui/qvalidator.html">QValidator</a></span> <span class="operator">*</span>validator <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtgui/qregularexpressionvalidator.html">QRegularExpressionValidator</a></span>(regExp<span class="operator">,</span> lineEdit);
          lineEdit<span class="operator">-</span><span class="operator">&gt;</span>setValidator(validator);
      }

      <span class="keyword">return</span> lineEdit;
  }

  <span class="type">void</span> VariantDelegate<span class="operator">::</span>setEditorData(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span>
                                      <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span>
  {
      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> value <span class="operator">=</span> index<span class="operator">.</span>model()<span class="operator">-</span><span class="operator">&gt;</span>data(index<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
      <span class="keyword">if</span> (<span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>lineEdit <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(editor))
          lineEdit<span class="operator">-</span><span class="operator">&gt;</span>setText(displayText(value));
  }

  <span class="type">void</span> VariantDelegate<span class="operator">::</span>setModelData(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>editor<span class="operator">,</span> <span class="type"><a href="../qtcore/qabstractitemmodel.html">QAbstractItemModel</a></span> <span class="operator">*</span>model<span class="operator">,</span>
                                     <span class="keyword">const</span> <span class="type"><a href="../qtcore/qmodelindex.html">QModelIndex</a></span> <span class="operator">&amp;</span>index) <span class="keyword">const</span>
  {
      <span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span>lineEdit <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span><span class="type"><a href="qlineedit.html">QLineEdit</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(editor);
      <span class="keyword">if</span> (<span class="operator">!</span>lineEdit<span class="operator">-</span><span class="operator">&gt;</span>isModified())
          <span class="keyword">return</span>;

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> lineEdit<span class="operator">-</span><span class="operator">&gt;</span>text();
      <span class="keyword">const</span> <span class="type"><a href="../qtgui/qvalidator.html">QValidator</a></span> <span class="operator">*</span>validator <span class="operator">=</span> lineEdit<span class="operator">-</span><span class="operator">&gt;</span>validator();
      <span class="keyword">if</span> (validator) {
          <span class="type">int</span> pos;
          <span class="keyword">if</span> (validator<span class="operator">-</span><span class="operator">&gt;</span>validate(text<span class="operator">,</span> pos) <span class="operator">!</span><span class="operator">=</span> <span class="type"><a href="../qtgui/qvalidator.html">QValidator</a></span><span class="operator">::</span>Acceptable)
              <span class="keyword">return</span>;
      }

      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> originalValue <span class="operator">=</span> index<span class="operator">.</span>model()<span class="operator">-</span><span class="operator">&gt;</span>data(index<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
      <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> value;
      <span class="type"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> match;

      <span class="keyword">switch</span> (originalValue<span class="operator">.</span>type()) {
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Char:
          value <span class="operator">=</span> text<span class="operator">.</span>at(<span class="number">0</span>);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Color:
          match <span class="operator">=</span> colorExp<span class="operator">.</span>match(text);
          value <span class="operator">=</span> <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(<a href="../qtcore/qtglobal.html#qMin">qMin</a>(match<span class="operator">.</span>captured(<span class="number">1</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> <span class="number">255</span>)<span class="operator">,</span>
                         <a href="../qtcore/qtglobal.html#qMin">qMin</a>(match<span class="operator">.</span>captured(<span class="number">2</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> <span class="number">255</span>)<span class="operator">,</span>
                         <a href="../qtcore/qtglobal.html#qMin">qMin</a>(match<span class="operator">.</span>captured(<span class="number">3</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> <span class="number">255</span>)<span class="operator">,</span>
                         <a href="../qtcore/qtglobal.html#qMin">qMin</a>(match<span class="operator">.</span>captured(<span class="number">4</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> <span class="number">255</span>));
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Date:
          {
              <span class="type"><a href="../qtcore/qdate.html">QDate</a></span> date <span class="operator">=</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span><span class="operator">::</span>fromString(text<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
              <span class="keyword">if</span> (<span class="operator">!</span>date<span class="operator">.</span>isValid())
                  <span class="keyword">return</span>;
              value <span class="operator">=</span> date;
          }
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>DateTime:
          {
              <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span> dateTime <span class="operator">=</span> <span class="type"><a href="../qtcore/qdatetime.html">QDateTime</a></span><span class="operator">::</span>fromString(text<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
              <span class="keyword">if</span> (<span class="operator">!</span>dateTime<span class="operator">.</span>isValid())
                  <span class="keyword">return</span>;
              value <span class="operator">=</span> dateTime;
          }
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Point:
          match <span class="operator">=</span> pointExp<span class="operator">.</span>match(text);
          value <span class="operator">=</span> <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span>(match<span class="operator">.</span>captured(<span class="number">1</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> match<span class="operator">.</span>captured(<span class="number">2</span>)<span class="operator">.</span>toInt());
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Rect:
          match <span class="operator">=</span> rectExp<span class="operator">.</span>match(text);
          value <span class="operator">=</span> <span class="type"><a href="../qtcore/qrect.html">QRect</a></span>(match<span class="operator">.</span>captured(<span class="number">1</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> match<span class="operator">.</span>captured(<span class="number">2</span>)<span class="operator">.</span>toInt()<span class="operator">,</span>
                        match<span class="operator">.</span>captured(<span class="number">3</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> match<span class="operator">.</span>captured(<span class="number">4</span>)<span class="operator">.</span>toInt());
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Size:
          match <span class="operator">=</span> sizeExp<span class="operator">.</span>match(text);
          value <span class="operator">=</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(match<span class="operator">.</span>captured(<span class="number">1</span>)<span class="operator">.</span>toInt()<span class="operator">,</span> match<span class="operator">.</span>captured(<span class="number">2</span>)<span class="operator">.</span>toInt());
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>StringList:
          value <span class="operator">=</span> text<span class="operator">.</span>split(<span class="char">','</span>);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Time:
          {
              <span class="type"><a href="../qtcore/qtime.html">QTime</a></span> time <span class="operator">=</span> <span class="type"><a href="../qtcore/qtime.html">QTime</a></span><span class="operator">::</span>fromString(text<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
              <span class="keyword">if</span> (<span class="operator">!</span>time<span class="operator">.</span>isValid())
                  <span class="keyword">return</span>;
              value <span class="operator">=</span> time;
          }
          <span class="keyword">break</span>;
      <span class="keyword">default</span>:
          value <span class="operator">=</span> text;
          value<span class="operator">.</span>convert(originalValue<span class="operator">.</span>type());
      }

      model<span class="operator">-</span><span class="operator">&gt;</span>setData(index<span class="operator">,</span> displayText(value)<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>DisplayRole);
      model<span class="operator">-</span><span class="operator">&gt;</span>setData(index<span class="operator">,</span> value<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>UserRole);
  }

  bool VariantDelegate<span class="operator">::</span>isSupportedType(<span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Type type)
  {
      <span class="keyword">switch</span> (type) {
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Bool:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>ByteArray:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Char:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Color:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Date:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>DateTime:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Double:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Int:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>LongLong:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Point:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Rect:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Size:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>String:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>StringList:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Time:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>UInt:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>ULongLong:
          <span class="keyword">return</span> <span class="keyword">true</span>;
      <span class="keyword">default</span>:
          <span class="keyword">return</span> <span class="keyword">false</span>;
      }
  }

  <span class="type"><a href="../qtcore/qstring.html">QString</a></span> VariantDelegate<span class="operator">::</span>displayText(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> <span class="operator">&amp;</span>value)
  {
      <span class="keyword">switch</span> (value<span class="operator">.</span>type()) {
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Bool:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>ByteArray:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Char:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Double:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Int:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>LongLong:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>String:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>UInt:
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>ULongLong:
          <span class="keyword">return</span> value<span class="operator">.</span>toString();
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Color:
          {
              <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color <span class="operator">=</span> qvariant_cast<span class="operator">&lt;</span><span class="type"><a href="../qtgui/qcolor.html">QColor</a></span><span class="operator">&gt;</span>(value);
              <span class="keyword">return</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;(%1,%2,%3,%4)&quot;</span>)
                     <span class="operator">.</span>arg(color<span class="operator">.</span>red())<span class="operator">.</span>arg(color<span class="operator">.</span>green())
                     <span class="operator">.</span>arg(color<span class="operator">.</span>blue())<span class="operator">.</span>arg(color<span class="operator">.</span>alpha());
          }
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Date:
          <span class="keyword">return</span> value<span class="operator">.</span>toDate()<span class="operator">.</span>toString(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>DateTime:
          <span class="keyword">return</span> value<span class="operator">.</span>toDateTime()<span class="operator">.</span>toString(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Invalid:
          <span class="keyword">return</span> <span class="string">&quot;&lt;Invalid&gt;&quot;</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Point:
          {
              <span class="type"><a href="../qtcore/qpoint.html">QPoint</a></span> point <span class="operator">=</span> value<span class="operator">.</span>toPoint();
              <span class="keyword">return</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;(%1,%2)&quot;</span>)<span class="operator">.</span>arg(point<span class="operator">.</span>x())<span class="operator">.</span>arg(point<span class="operator">.</span>y());
          }
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Rect:
          {
              <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> rect <span class="operator">=</span> value<span class="operator">.</span>toRect();
              <span class="keyword">return</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;(%1,%2,%3,%4)&quot;</span>)
                     <span class="operator">.</span>arg(rect<span class="operator">.</span>x())<span class="operator">.</span>arg(rect<span class="operator">.</span>y())
                     <span class="operator">.</span>arg(rect<span class="operator">.</span>width())<span class="operator">.</span>arg(rect<span class="operator">.</span>height());
          }
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Size:
          {
              <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> size <span class="operator">=</span> value<span class="operator">.</span>toSize();
              <span class="keyword">return</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;(%1,%2)&quot;</span>)<span class="operator">.</span>arg(size<span class="operator">.</span>width())<span class="operator">.</span>arg(size<span class="operator">.</span>height());
          }
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>StringList:
          <span class="keyword">return</span> value<span class="operator">.</span>toStringList()<span class="operator">.</span>join(<span class="char">','</span>);
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span><span class="operator">::</span>Time:
          <span class="keyword">return</span> value<span class="operator">.</span>toTime()<span class="operator">.</span>toString(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>ISODate);
      <span class="keyword">default</span>:
          <span class="keyword">break</span>;
      }
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;&lt;%1&gt;&quot;</span>)<span class="operator">.</span>arg(value<span class="operator">.</span>typeName());
  }

</pre>
</div>
<!-- @@@tools/settingseditor/variantdelegate.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
